﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GuoShuCan.SDK.Web.ServerControls.Manage;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using GuoShuCan.SDK.Web.Globalization;
public partial class Manage_EditPhotoManagement : EditUserControl, IEditObject
{
    #region IEditObject
    /// <summary>
    /// 编辑对象接口。
    /// </summary>
    object IEditObject.EditObject
    {
        get
        {
            return null;
        }
        set
        {
            //设置要编辑的数码产品ID。
            this.ViewState["EditID"] = Convert.ToInt32(value);
            //读取数据库数据数码产品名称与图片地址。
            using (OleDbConnection conn = new OleDbConnection(DataConfig.ConnectionString))
            {
                using (OleDbCommand comm = conn.CreateCommand())
                {
                    try
                    {
                        //按照制定语言读取数据库。
                        switch (ILanguage.Language)
                        {
                            case "zh-CN":
                                comm.CommandText = "SELECT Name, Photo FROM CNList WHERE (ID = " + value.ToString() + ")";
                                break;
                            case "zh-TW":
                                comm.CommandText = "SELECT Name, Photo FROM TWList WHERE (ID = " + value.ToString() + ")";
                                break;
                            case "en-US":
                                comm.CommandText = "SELECT Name, Photo FROM USList WHERE (ID = " + value.ToString() + ")";
                                break;
                            case "ja-JP":
                                comm.CommandText = "SELECT Name, Photo FROM JPList WHERE (ID = " + value.ToString() + ")";
                                break;
                            case "ko-KR":
                                comm.CommandText = "SELECT Name, Photo FROM KRList WHERE (ID = " + value.ToString() + ")";
                                break;
                        }
                        comm.CommandType = CommandType.Text;
                        conn.Open();
                        //读取数码产品编辑行内容。
                        using (OleDbDataReader r = comm.ExecuteReader())
                        {
                            if (r.Read())
                            {
                                //读取数码产品编辑行名称。
                                this.ViewState["EditName"] = r.GetString(0);
                                //读取数码产品编辑行主图片。
                                if (r.IsDBNull(1))
                                {
                                    //没有编辑行图片。
                                    this.ViewState["Photo"] = null;
                                    //this.Photo.ImageUrl = "~/";
                                }
                                else
                                {
                                    //有编辑行图片。
                                    this.ViewState["Photo"] = r.GetString(1);
                                    this.Photo.ImageUrl = "~/Upload/" + r.GetString(1);
                                }
                            }
                            else
                            {
                                conn.Close();
                                this.OnBack();
                                this.UpdateComplete();
                                return;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        conn.Close();
                        //返回错误消息。
                        this.ScriptManager.AsyncPostBackErrorMessage = ex.Message;
                        this.OnBack();
                        this.UpdateComplete();
                        return;
                    }
                }
            }
            this.OnUpdateCulture();
            this.OnUpdateTitle();
        }
    }
    #endregion
    /// <summary>
    /// 初始化控件。
    /// </summary>
    /// <param name="e"></param>
    protected override void OnInit(EventArgs e)
    {
        //重载基类。
        base.OnInit(e);
        //注册异步等待按钮对象。
        this.RegisterAsyncListControl(this.Back);
        this.RegisterAsyncListControl(this.Save);
        //
        //
        this.editTool.Visible = true;
        this.photoTool.Visible = false;
        this.descriptionTool.Visible = true;
        backTool.Visible = true;
    }
    protected LinkButton backTool = null;
    protected LinkButton editTool = null;
    protected LinkButton photoTool = null;
    protected LinkButton descriptionTool = null;
    /// <summary>
    /// 初始化工具栏。
    /// </summary>
    /// <remarks>初始化工具栏。</remarks>
    protected override void OnInitTool()
    {
        //添加快捷工具栏。
        if (this.Master.CenterHeadLeftCellUpdatePanel.ContentTemplateContainer.HasControls())
        {
            //编辑主要参数
            editTool = (LinkButton)this.Master.CenterHeadLeftCellUpdatePanel.ContentTemplateContainer.FindControl("editTool");
            editTool.Visible = false;
            editTool.Click += new EventHandler(delegate(object o, EventArgs ee)
            {
                ((IEditObject)this.EditControl.LoadEditControl("EditManagement.ascx")).EditObject = this.ViewState["EditID"];
                this.UpdateComplete();
            });
            //编辑主图片。
            photoTool = (LinkButton)this.Master.CenterHeadLeftCellUpdatePanel.ContentTemplateContainer.FindControl("photoTool");
            photoTool.Visible = false;
            photoTool.Click += new EventHandler(delegate(object o, EventArgs ee)
            {
                ((IEditObject)this.EditControl.LoadEditControl("EditPhotoManagement.ascx")).EditObject = this.ViewState["EditID"];
                this.UpdateComplete();
            });
            //编辑附加图片。
            descriptionTool = (LinkButton)this.Master.CenterHeadLeftCellUpdatePanel.ContentTemplateContainer.FindControl("descriptionTool");
            descriptionTool.Visible = false;
            descriptionTool.Click += new EventHandler(delegate(object o, EventArgs ee)
            {
                ((IEditObject)this.EditControl.LoadEditControl("EditDescriptionManagement.ascx")).EditObject = this.ViewState["EditID"];
                this.UpdateComplete();
            });
            //返回按钮。
            backTool = (LinkButton)this.Master.CenterHeadLeftCellUpdatePanel.ContentTemplateContainer.FindControl("backTool");
            backTool.Click += new EventHandler(delegate(object o, EventArgs ee)
            {
                this.OnBack();
                //异步加载等待完成。
                this.UpdateComplete();
            });
        }
        else
        {
            Label l = new Label();
            l.Text = "&nbsp;&nbsp;[&nbsp;";
            //
            l.Font.Size = new FontUnit("12px");
            l.Font.Underline = false;
            l.ForeColor = Color.White;
            this.Master.CenterHeadLeftCellUpdatePanel.ContentTemplateContainer.Controls.Add(l);
            //编辑主要参数
            editTool = new LinkButton();
            editTool.ID = "editTool";
            editTool.Visible = false;
            editTool.CssClass = "tool_a";
            editTool.Click += new EventHandler(delegate(object o, EventArgs ee)
            {
                ((IEditObject)this.EditControl.LoadEditControl("EditManagement.ascx")).EditObject = this.ViewState["EditID"];
                this.UpdateComplete();
            });
            this.Master.CenterHeadLeftCellUpdatePanel.ContentTemplateContainer.Controls.Add(editTool);
            l = new Label();
            l.Text = "&nbsp;";
            //
            l.Font.Size = new FontUnit("12px");
            this.Master.CenterHeadLeftCellUpdatePanel.ContentTemplateContainer.Controls.Add(l);
            //编辑主图片。
            photoTool = new LinkButton();
            photoTool.Visible = false;
            photoTool.ID = "photoTool";
            photoTool.CssClass = "tool_a";
            photoTool.Click += new EventHandler(delegate(object o, EventArgs ee)
            {
                ((IEditObject)this.EditControl.LoadEditControl("EditPhotoManagement.ascx")).EditObject = this.ViewState["EditID"];
                this.UpdateComplete();
            });
            this.Master.CenterHeadLeftCellUpdatePanel.ContentTemplateContainer.Controls.Add(photoTool);
            l = new Label();
            l.Text = "&nbsp;";
            //
            l.Font.Size = new FontUnit("12px");
            this.Master.CenterHeadLeftCellUpdatePanel.ContentTemplateContainer.Controls.Add(l);
            //编辑附加图片。
            descriptionTool = new LinkButton();
            descriptionTool.Visible = false;
            descriptionTool.ID = "descriptionTool";
            descriptionTool.CssClass = "tool_a";
            descriptionTool.Click += new EventHandler(delegate(object o, EventArgs ee)
            {
                ((IEditObject)this.EditControl.LoadEditControl("EditDescriptionManagement.ascx")).EditObject = this.ViewState["EditID"];
                this.UpdateComplete();
            });
            this.Master.CenterHeadLeftCellUpdatePanel.ContentTemplateContainer.Controls.Add(descriptionTool);
            l = new Label();
            l.Text = "&nbsp;";
            //
            l.Font.Size = new FontUnit("12px");
            this.Master.CenterHeadLeftCellUpdatePanel.ContentTemplateContainer.Controls.Add(l);
            //返回按钮。
            backTool = new LinkButton();
            backTool.ID = "backTool";
            backTool.CssClass = "tool_a";
            backTool.Click += new EventHandler(delegate(object o, EventArgs ee)
            {
                this.OnBack();
                //异步加载等待完成。
                this.UpdateComplete();
            });
            this.Master.CenterHeadLeftCellUpdatePanel.ContentTemplateContainer.Controls.Add(backTool);
            l = new Label();
            l.Text = "&nbsp;]";
            //
            l.Font.Size = new FontUnit("12px");
            l.Font.Underline = false;
            l.ForeColor = Color.White;
            this.Master.CenterHeadLeftCellUpdatePanel.ContentTemplateContainer.Controls.Add(l);
        }
        //
        editTool.Font.Size = new FontUnit("12px");
        editTool.Font.Underline = false;
        editTool.ForeColor = Color.White;
        //
        backTool.Font.Size = new FontUnit("12px");
        backTool.Font.Underline = false;
        backTool.ForeColor = Color.White;
        //
        descriptionTool.Font.Size = new FontUnit("12px");
        descriptionTool.Font.Underline = false;
        descriptionTool.ForeColor = Color.White;
        //
        photoTool.Font.Size = new FontUnit("12px");
        photoTool.Font.Underline = false;
        photoTool.ForeColor = Color.White;

        //
        this.RegisterAsyncListControl(editTool);
        this.RegisterAsyncListControl(backTool);
        this.RegisterAsyncListControl(photoTool);
        this.RegisterAsyncListControl(descriptionTool);
        //
        editTool.Text = this.GetGlobalResourceObject("Tool", "editTool").ToString();
        photoTool.Text = this.GetGlobalResourceObject("Tool", "photoTool").ToString();
        descriptionTool.Text = this.GetGlobalResourceObject("Tool", "descriptionTool").ToString();
        backTool.Text = this.GetGlobalResourceObject("Tool", "backTool").ToString();
        //
        this.Master.CenterHeadLeftCellUpdatePanel.Update();
    }
    /// <summary>
    /// 呈现控件。
    /// </summary>
    /// <param name="e"></param>
    protected override void OnPreRender(EventArgs e)
    {
        //重写基类。
        base.OnPreRender(e);
        //设置按钮图片。
        string url = "~/App_Themes/Manage_Default/images/" + this.ILanguage.Language + "/";
        this.Back.ImageUrl = url + "BackButton.gif";
        this.Save.ImageUrl = url + "SaveButton.gif";
    }
    /// <summary>
    /// 更新浏览器标题。
    /// </summary>
    protected override void OnUpdateTitle()
    {
        //验证数码产品名称。
        if (this.ViewState["EditName"] == null)
            //更新不带数码产品名称标题。
            this.Page.Title = ((IGetLocalResourceObject)this.Page).GetLocalResourceObject("Title").ToString() + (" - [ " + ((IGetLocalResourceObject)this.EditControl.ListPlaceHolder.Controls[0]).GetLocalResourceObject("Title").ToString() + " ]") + (" - [ " + this.GetLocalResourceObject("Title").ToString() + " ]");
        else
            //更新带数码产品名称标题。
            this.Page.Title = ((IGetLocalResourceObject)this.Page).GetLocalResourceObject("Title").ToString() + (" - [ " + ((IGetLocalResourceObject)this.EditControl.ListPlaceHolder.Controls[0]).GetLocalResourceObject("Title").ToString() + " ]") + (" - [ " + this.GetLocalResourceObject("Title").ToString() + " ]") + (" - [ " + this.ViewState["EditName"].ToString() + " ]");
    }
    /// <summary>
    /// 保存修改按钮事件。
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Save_Click(object sender, ImageClickEventArgs e)
    {
        //图片文件。
        string _photoUrl = string.Empty;
        //验证是否拥有图片。
        if (this.FileUploadLocalPhoto.HasFile)
        {
            //拥有图片处理，验证图片数据是否有效与长度。
            if (this.FileUploadLocalPhoto.PostedFile != null && this.FileUploadLocalPhoto.PostedFile.ContentLength > 0)
            {
                //获取图片数据长度。
                int upFileSize = this.FileUploadLocalPhoto.PostedFile.ContentLength;
                //设置图片数据长度，
                this.LocalPhotoError.Text = (upFileSize / 1024).ToString() + " KB";
                //获取图片扩展名。
                string extFileString = System.IO.Path.GetExtension(this.FileUploadLocalPhoto.PostedFile.FileName).ToLower();
                //验证上传图片格式。
                if (!(extFileString == ".jpg" || extFileString == ".png" || extFileString == ".bmp" || extFileString == ".gif"))
                {
                    //图片格式错误。
                    this.LocalPhotoError.Text = this.GetLocalResourceObject("FileUploadLocalPhotoExtensionError").ToString();
                    //异步加载等待完成。
                    this.UpdateComplete();
                    return;
                }
                //生产图片存储文件名称。
                string saveFileName = Guid.NewGuid().ToString().Replace("-", "").ToLower();
                //保存文件。
                this.FileUploadLocalPhoto.PostedFile.SaveAs(Server.MapPath("..\\Upload\\" + saveFileName + extFileString));
                //生成图片存储名称。
                _photoUrl = saveFileName + extFileString;
            }
        }
        else
        {
            //设置图片为空错误信息。
            this.LocalPhotoError.Text = this.GetLocalResourceObject("FileUploadLocalPhotoNullError").ToString();
            //异步加载等待完成。
            this.UpdateComplete();
            return;
        }
        //验证是否存在旧的图片。
        if (this.ViewState["Photo"] != null)
        {
            DeletePhoto(this.ViewState["Photo"].ToString());
        }
        //设置新的图片文件名称。
        this.ViewState["Photo"] = _photoUrl;
        //设置显示新的图片。
        this.Photo.ImageUrl = "~/Upload/" + _photoUrl;
        //保存数据库数据
        using (OleDbConnection conn = new OleDbConnection(DataConfig.ConnectionString))
        {
            using (OleDbCommand comm = conn.CreateCommand())
            {
                try
                {
                    //按照制定语言更新数据库。
                    switch (this.ILanguage.Language)
                    {
                        case "zh-CN":
                            comm.CommandText = "UPDATE CNList Set Photo = '" + _photoUrl + "' WHERE (ID = " + this.ViewState["EditID"].ToString() + ")";
                            break;
                        case "zh-TW":
                            comm.CommandText = "UPDATE TWList Set Photo = '" + _photoUrl + "' WHERE (ID = " + this.ViewState["EditID"].ToString() + ")";
                            break;
                        case "en-US":
                            comm.CommandText = "UPDATE USList Set Photo = '" + _photoUrl + "' WHERE (ID = " + this.ViewState["EditID"].ToString() + ")";
                            break;
                        case "ja-JP":
                            comm.CommandText = "UPDATE JPList Set Photo = '" + _photoUrl + "' WHERE (ID = " + this.ViewState["EditID"].ToString() + ")";
                            break;
                        case "ko-KR":
                            comm.CommandText = "UPDATE KRList Set Photo = '" + _photoUrl + "' WHERE (ID = " + this.ViewState["EditID"].ToString() + ")";
                            break;
                    }
                    comm.CommandType = CommandType.Text;
                    conn.Open();
                    if (comm.ExecuteNonQuery() == 0)
                    {
                        conn.Close();
                        DeletePhoto(_photoUrl);
                        //更新ID丢失侧返回编辑列表。
                        this.OnBack();
                    }
                }
                catch (Exception ex)
                {
                    conn.Close();
                    DeletePhoto(_photoUrl);
                    this.OnBack();
                    //返回错误消息。
                    this.ScriptManager.AsyncPostBackErrorMessage = ex.Message;
                }
            }
        }
        //异步加载等待完成。
        this.UpdateComplete();
    }
    /// <summary>
    /// 删除图片
    /// </summary>
    /// <param name="photoUrl"></param>
    void DeletePhoto(string photoUrl)
    {
        if (photoUrl != string.Empty)
        {
            //存在旧的图片删除它。
            try
            {
                //创建删除图片路径。
                string deleteLocalPhoto = this.Server.MapPath("~/Upload/" + photoUrl);
                //验证图片是否存在磁盘。
                if (System.IO.File.Exists(deleteLocalPhoto))
                {
                    //图片存在删除它。
                    System.IO.File.Delete(deleteLocalPhoto);
                }
            }
            catch (Exception) { }
        }
    }
}